package com.jiutong.meipenggang.ui.activitys;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.entity.RegisterBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.StringUtil;
import com.jiutong.meipenggang.utils.TimerUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/5/27.
 */

public class RegisterActivity extends BaseActivity implements RegisterAndLoginBiz {


    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.iv_register_phone)
    ImageView ivRegisterPhone;
    @Bind(R.id.ed_phone_num)
    EditText edPhoneNum;
    @Bind(R.id.view_register_phone)
    View viewRegisterPhone;
    @Bind(R.id.iv_register_lock_pwd)
    ImageView ivRegisterLockPwd;
    @Bind(R.id.ed_password)
    EditText edPassword;
    @Bind(R.id.view_register_pwd)
    View viewRegisterPwd;
    @Bind(R.id.iv_register_lock_pwd_confirm)
    ImageView ivRegisterLockPwdConfirm;
    @Bind(R.id.ed_password_confirm)
    EditText edPasswordConfirm;
    @Bind(R.id.view_register_pwd_confirm)
    View viewRegisterPwdConfirm;
    @Bind(R.id.iv_register_code)
    ImageView ivRegisterCode;
    @Bind(R.id.ed_identify_code)
    EditText edIdentifyCode;
    @Bind(R.id.tv_vercode)
    TextView tvVercode;
    @Bind(R.id.view_register_verify)
    View viewRegisterVerify;
    @Bind(R.id.iv_aggree)
    ImageView ivAggree;
    @Bind(R.id.tv_register_agreement)
    TextView tvRegisterAgreement;
    @Bind(R.id.tv_agreement)
    TextView tvAgreement;
    @Bind(R.id.btn_register)
    Button btnRegister;
    private UserInfoPresent userInfoPresent;

    private TimerUtil timerUtil;
    private boolean isCheck = false;
    private boolean isOk = true;
    //是获取验证码，还是注册 1，获取验证码，3注册
    private boolean isRegist = false;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("注册");

        //初始化倒计时
        timerUtil = new TimerUtil(tvVercode);
        timerUtil.setCountTimer();

        //EditText监听事件
        etChangeListener();

        userInfoPresent = new UserInfoPresent(this, this);
    }


    //EditText监听事件
    private void etChangeListener() {
        //手机号
        edPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivRegisterPhone.setBackgroundResource(R.mipmap.ic_phone_pre);
                    viewRegisterPhone.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivRegisterPhone.setBackgroundResource(R.mipmap.ic_phone);
                    viewRegisterPhone.setBackgroundResource(R.color.line);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //密码
        edPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null & s.length() > 0) {
                    ivRegisterLockPwd.setBackgroundResource(R.mipmap.ic_lock_pre);
                    viewRegisterPwd.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivRegisterLockPwd.setBackgroundResource(R.mipmap.ic_lock);
                    viewRegisterPwd.setBackgroundResource(R.color.line);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //确认密码
        edPasswordConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivRegisterLockPwdConfirm.setBackgroundResource(R.mipmap.ic_lock_pre);
                    viewRegisterPwdConfirm.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivRegisterLockPwdConfirm.setBackgroundResource(R.mipmap.ic_lock);
                    viewRegisterPwdConfirm.setBackgroundResource(R.color.line);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //验证码
        edIdentifyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivRegisterCode.setBackgroundResource(R.mipmap.ic_code_pre);
                    viewRegisterVerify.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivRegisterCode.setBackgroundResource(R.mipmap.ic_code);
                    viewRegisterVerify.setBackgroundResource(R.color.line);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.rl_common_title_back, R.id.tv_vercode,R.id.tv_register_agreement, R.id.tv_agreement, R.id.btn_register,R.id.iv_aggree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.tv_vercode:
                if (isRightPhone()) {
                    isRegist =false;
                    userInfoPresent.getAuthCode();
                }
                break;
            case R.id.tv_register_agreement:
            case R.id.iv_aggree:
                if(isCheck){
                    ivAggree.setImageResource(R.mipmap.register_choice);
                    isOk = false;
                    isCheck =false;
                }else{
                    ivAggree.setImageResource(R.mipmap.register_choice_pre);
                    isOk = true;
                    isCheck =true;
                }
                break;
            case R.id.tv_agreement:
                openActivity(AgreementActivity.class);
                break;
            case R.id.btn_register:
                if (isRightAction())
                    isRegist = true;
                    userInfoPresent.register();
                break;
        }
    }

    /**
     * 手机号是否输入正确
     */
    private boolean isRightPhone() {
        if (TextUtils.isEmpty(getPhoneNum())) {
            toastShortShow("请输入手机号码");
            return false;
        } else if (!StringUtil.isMobileNumber(getPhoneNum())) {
            toastShortShow("手机号输入有误");
            return false;
        }
        return true;
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {
        if (!isRightPhone()) {
            return false;
        }

        if (TextUtils.isEmpty(getOriPwd())) {
            toastShortShow("请输入密码");
            return false;
        }
        if (!StringUtil.isRightPwd(getOriPwd())) {
            toastShortShow("密码格式错误");
            return false;
        }

        if (TextUtils.isEmpty(getNewPwd())) {
            toastShortShow("请输入确认密码");
            return false;
        }

        if (!getOriPwd().equals(edPasswordConfirm.getText().toString().trim())) {
            toastShortShow("两次输入密码不一致，请重新输入");
            edPassword.setText("");
            edPasswordConfirm.setText("");
            return false;
        }

        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入手机验证码");
            return false;
        }

        if(!isOk){
            toastShortShow("请阅读并同意美鹏港服务协议");
            return false;
        }

        return true;
    }

    @Override
    public String getPhoneNum() {
        return edPhoneNum.getText().toString().trim();
    }


    @Override
    public String getVerificatCode() {
        return edIdentifyCode.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return edPassword.getText().toString().trim();
    }

    @Override
    public String getNewPwd() {
        return edPasswordConfirm.getText().toString().trim();
    }


    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {
                if (subjects.getError()==-1) {
                    timerUtil.startTimer();
                    toastShortShow(subjects.getMsg());
//            identifyCode = subjects.getData();
                } else {
                    toastShortShow(subjects.getMsg());
                }

    }

    @Override
    public void onSuccess(Object object) {
        RegisterBean registerBean = (RegisterBean) object;
        if (registerBean.getError()==-1) {
            toastShortShow(registerBean.getMsg());
//        SpConfig.getInstance().putString("sessionId", respond.getSessionId());
            SpConfig.getInstance().edit().putString(Constants.USERNAME_STRING, getPhoneNum()).commit();
            SpConfig.getInstance().edit().putString(Constants.TEPPHONE_STRING, getPhoneNum()).commit();
            SpConfig.getInstance().edit().putString(Constants.ID,registerBean.getId()).commit();
            SpConfig.getInstance().edit().putString(Constants.IMAGE_PATH,"").commit();
            SpConfig.getInstance().edit().putString(Constants.SHOU_SHI,"").commit();
            SpConfig.getInstance().edit().putBoolean(Constants.IS_LOGIN, true).commit();
            openActivity(GestureEditActivity.class);
            finish();
        } else {
            toastShortShow(registerBean.getMsg());
        }


    }



    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        if(isRegist){
            return 3;
        }else {
            return 1;
        }

    }


}
